<template>
  <div>
    <el-form-item label="频道" prop="channelId">
      <el-select
        v-model="channelId"
        clearable
        placeholder="请选择"
        @change="hChannelsChange"
      >
        <el-option
          v-for="item in channels"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </el-form-item>
  </div>
</template>

<script>
export default {
  name: 'myChannel',
  props: ['value'],
  data () {
    return {
      channels: [],
      channelId: null
    };
  },
  methods: {
    async getChannelId () {
      try {
        const res = await this.$axios({
          url: '/mp/v1_0/channels',
          method: 'GET'
        });
        console.log(res);
        this.channels = res.data.data.channels;
      } catch (err) {
        console.log(err);
      }
    },
    hChannelsChange (val) {
      this.$emit('input', val);
      console.log(this.value);
    }
  },
  // 侦听器，当value的值发生变化时触发
  watch: {
    // value的值是父组件传进来的，当获取文章详情时，将文章的分类传进来，要实时更新数据，渲染到页面中
    value (newValue, oleValue) {
      console.log(newValue, oleValue);
      this.channelId = newValue
    }
  },
  created () {
    this.getChannelId();
  }
};
</script>

<style lang="scss" scoped>
</style>
